﻿using AppVulneravel.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AppVulneravel.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            if (Request.Cookies["Login"] != null)
            {
                HttpCookie myCookie = Request.Cookies["Login"];
                List<Contatos> l = new Contatos().CarregarLista(Convert.ToInt32(myCookie["Id"]));
                return View(l);
            }

            return RedirectToAction("Login");
        }

        //
        // GET: /Home/Create

        public ActionResult CriarContato()
        {
            return View();
        }

        //
        // POST: /Home/Create

        [HttpPost]
        public ActionResult CriarContato(Contatos c)
        {
            try
            {
                c.IdUsuario = Convert.ToInt32(Request.Cookies["Login"]["Id"]);
                c.Incluir();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult AlteraContato(int id)
        {
            Contatos c = new Contatos();
            c.Carregar(id);
            return View(c);
        }

        [HttpPost]
        public ActionResult AlteraContato(int id, Contatos c)
        {
            try
            {
                c.Alterar();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Home/Delete/5

        public ActionResult ExcluiContato(int id)
        {
            Contatos c = new Contatos();
            c.Excluir(id);
            return RedirectToAction("Index");
        }

        //
        // POST: /Home/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(Usuario _us)
        {
            Usuario us = new Usuario();
            if (us.CarregarLogin(_us.Login, _us.Senha))
            {
                HttpCookie cookie = new HttpCookie("login");

                //Define o valor do cookie
                cookie["Nome"] = us.Nome;
                cookie["Login"] = us.Login;
                cookie["Id"] = us.Id.ToString();
                cookie["Senha"] = us.Senha;
                cookie["Email"] = us.Email;


                //Time para expiração (1min)
                DateTime dtNow = DateTime.Now;
                //TimeSpan tsMinute = new TimeSpan(0, 0, 1, 0);
                //cookie.Expires = dtNow + tsMinute;

                //Adiciona o cookie
                Response.Cookies.Add(cookie);

                return RedirectToAction("Index");
            }
            else
            {
                ViewBag.Erro = "Usuário ou senha incorretos.";
                return View();
            }

        }

        [AllowAnonymous]
        public ActionResult Logout()
        {
            if (Request.Cookies["Login"] != null)
            {
                // Apaga o cookie
                HttpCookie myCookie = Request.Cookies["Login"];
                myCookie.Expires = DateTime.Now.AddDays(-1d);
                Response.Cookies.Add(myCookie);
            }

            return RedirectToAction("Login");
        }

        public ActionResult PesquisarContato(string q)
        {
            List<Contatos> l;
            if (q == null)
            {
                 l = new List<Contatos>();
            }
            else
            {
                l = new Contatos().BuscarPorNome(q);
            }
            return View(l);
        }
    }
}
